package com.galeno.zoopeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

/**
 * @author galeno @Title: @Description:
 * @date 2021/7/319:25
 */
public class Damo8Watch {
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    //
    ZooKeeper zk = new ZooKeeper("galeno01:2181", 1000, null);

    // 获取子节点的API
    List<String> children =
        zk.getChildren(
            "/",
            new Watcher() {
              @Override
              public void process(WatchedEvent event) {
                String path = event.getPath();
                Event.EventType type = event.getType();
                System.out.println("节点的个数发生变化");
                List<String> children1 = null;
                try {
                  children1 = zk.getChildren(path, this);
                } catch (KeeperException e) {
                  e.printStackTrace();
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                //System.out.println(type);
                System.out.println("变化后的节点有" + children1);
              }
            });
    System.out.println(children);
    Thread.sleep(Long.MAX_VALUE);
  }
}
